package com.gengzp.system.service.impl;

import com.gengzp.common.exception.BusinessException;
import com.gengzp.common.model.message.req.PageMessageReq;
import com.gengzp.common.model.message.resp.SearchMessageResp;
import com.gengzp.common.model.pagination.req.PaginationReq;
import com.gengzp.common.model.pagination.resp.PaginationResponse;
import com.gengzp.mq.client.enums.MqMessageStatusEnum;
import com.gengzp.mq.client.model.generic.MqPaginationResult;
import com.gengzp.mq.client.model.message.MqPageMessageResp;
import com.gengzp.mq.client.model.topic.MqTopicResp;
import com.gengzp.mq.client.utils.MqMessageUtils;
import com.gengzp.system.service.MqService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName MqServiceImpl
 * @Description 消息队列管理模块服务层实现类
 * @Author gengzp
 * @Date 2025/9/29 18:07
 */
@Service
public class MqServiceImpl implements MqService {

    private static final Logger logger = LoggerFactory.getLogger(MqServiceImpl.class);

    @Override
    public void createTopic(String topicName, String description) {
        MqMessageUtils.createTopic(topicName, description);
    }

    @Override
    public void editTopicDescription(String topicName, String description) {
        MqMessageUtils.editTopicDescription(topicName, description);
    }

    @Override
    public void removeTopic(String topicName) {
        MqMessageUtils.removeTopic(topicName);
    }

    @Override
    public List<MqTopicResp> searchAllTopic() {
        return MqMessageUtils.searchAllTopic();
    }

    @Override
    public void resendMessage(String messageId) {
        MqMessageUtils.resendMessage(messageId);
    }

    @Override
    public SearchMessageResp searchMessageById(String messageId) {
        MqPaginationResult<MqPageMessageResp> pageMessageResult = MqMessageUtils.pageQueryMessage(
                1,
                1,
                messageId,
                null,
                null);
        if (CollectionUtils.isEmpty(pageMessageResult.getRecords())) {
            throw BusinessException.get("当前消息id不存在");
        }

        MqPageMessageResp mqPageMessageResp = pageMessageResult.getRecords().get(0);
        SearchMessageResp result = new SearchMessageResp();
        result.setMessageId(mqPageMessageResp.getMessageId());
        result.setContent(mqPageMessageResp.getContent());

        return result;
    }

    @Override
    public PaginationResponse<MqPageMessageResp> pageQueryMqMessages(PageMessageReq req) {
        MqMessageStatusEnum mqMessageStatusEnum = MqMessageStatusEnum.getByCode(req.getStatus());
        if (Objects.isNull(mqMessageStatusEnum)) {
            throw BusinessException.get(String.format("消息状态 status 枚举值: %s 不合法", req.getStatus()));
        }

        // 通过消息客户端工具类直接查询
        MqPaginationResult<MqPageMessageResp> pageMessageResult = MqMessageUtils.pageQueryMessage(
                req.getPageNum(),
                req.getPageSize(),
                req.getMessageId(),
                req.getTopic(),
                mqMessageStatusEnum);

        PaginationReq paginationReq = new PaginationReq();
        paginationReq.setPageNum(pageMessageResult.getPageNum());
        paginationReq.setPageSize(pageMessageResult.getPageSize());
        paginationReq.setTotal(pageMessageResult.getTotal());
        return PaginationResponse.get(pageMessageResult.getRecords(), paginationReq);
    }

}
